package com.iuie.ns.devtools.to;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.iuie.ns.devtools.element.BaseDevElement;
import com.iuie.ns.devtools.element.DevElement;
import com.iuie.ns.frame.utils.StringUtils;

public abstract class DevBodyTO {
	
	public abstract Long getId();

	public abstract String getFieldName();
	
	public abstract String getFieldType();
	
	public abstract String getComment();
	
	public abstract Boolean isIdAttribute();
	
	public abstract Boolean isDoQueryAttribute();
	
	public abstract String getQueryMode();
	
	public abstract Boolean isDoUpdateAttribute();
	
	public abstract String getDisplay();
	
	public abstract Integer getFieldLength();
	
	public abstract Boolean allowNull();
	
	@JsonIgnore
	public String getAttributeName() {
		return StringUtils.fieldNameToAttributeName(getFieldName());
	}
	
	@JsonIgnore
	private String methodName() {
		String attributeName = getAttributeName();
		String prefix = StringUtils.upperCase(attributeName.substring(0, 1));
		String suffix = attributeName.substring(1);
		return prefix + suffix;
	}
	
	@JsonIgnore
	public String getGetMethodName() {
		return "get" + methodName();
	}
	
	@JsonIgnore
	public String getSetMethodName() {
		return "set" + methodName();
	}
	
	@JsonIgnore
	public String getItemName(boolean withItem) {
		if (withItem) {
			return "item." + this.getAttributeName();
		}
		return this.getAttributeName();
	}
	
	@JsonIgnore
	public String getValueItem(boolean withItem) {
		String fieldType = this.getFieldType();
		if (StringUtils.equals(this.getFieldType(), "DATE")) {
			fieldType = "TIMESTAMP";
		}
		StringBuilder sb = new StringBuilder(12);
		sb.append("#{");
		sb.append(this.getItemName(withItem))
			.append(", ")
			.append("jdbcType=")
			.append(fieldType)
			.append("}");
		return sb.toString();
	}
	
	@JsonIgnore
	public DevElement getValueItemElement(boolean withItem) {
		String s = this.getValueItem(withItem);
		return new BaseDevElement(s);
	}
	
	/**
	 * 获取字符串形如： user_id=#{userId, jdbcType=BIGINT} 
	 * @return
	 */
	@JsonIgnore
	public DevElement getMyBatisEqualElement(boolean withItem) {
		return new BaseDevElement(getFieldName() + " = " + this.getValueItem(withItem));
	}
	
	/**
	 * 获取查询条件字符串：field_name like/=#{fieldName, jdbcType=FIELD_TYPE}
	 * @return
	 */
	@JsonIgnore
	public String getQueryCondition() {
		StringBuilder sb = new StringBuilder(12);
		sb.append("and ")
			.append(this.getFieldName());
		if (StringUtils.equals(this.getQueryMode(), "1")) {
			sb.append(" like concat(concat('%', ");
		} else {
			sb.append("=");
		}
		sb.append(this.getValueItem(true));
		if (StringUtils.equals(this.getQueryMode(), "1")) {
			sb.append("), '%')");
		}
		return sb.toString();
	}
	
	/**
	 * 获取查询条件节点形如：field_name like/=#{fieldName, jdbcType=FIELD_TYPE}
	 * @return
	 */
	@JsonIgnore
	public DevElement getQueryConditionElement() {
		return new BaseDevElement(this.getQueryCondition());
	}
	
	/**
	 * 获取节点形如：<br>
	 * &lt;if test="item.userId != null and item.userId != ''"&gt;<br>
	 * &nbsp;&nbsp;&nbsp;&nbsp;user_id=#{item.userId,j dbcType=BIGINT}<br>
	 * &lt;/if&gt;
	 * @return
	 */
	@JsonIgnore
	public DevElement getTestConditionElement() {
		StringBuilder sb = new StringBuilder(12);
		sb.append("\n\t\t<if test=\"")
			.append(this.getItemName(true))
			.append(" != null and ")
			.append(this.getItemName(true))
			.append(" != ''\">\n\t\t\t");
		return new BaseDevElement(sb.toString() , this.getQueryConditionElement(), "\n\t\t</if>");
	}
	
	@JsonIgnore
	public DevElement getMySQLFieldElement() {
		StringBuilder sb = new StringBuilder(12);
		sb.append("\t")
			.append(this.getFieldName())
			.append(" ")
			.append(this.getFieldType());
		if (this.getFieldLength() !=null && this.getFieldLength() != 0) {
			sb.append("(")
				.append(this.getFieldLength())
				.append(")");
		}
		if (!this.allowNull()) {
			sb.append(" ")
				.append("not null");
		}
		if (StringUtils.isNotBlank(this.getComment())) {
			sb.append(" ")
				.append("comment '")
				.append(this.getComment())
				.append("'");
		}
		sb.append(",\n");
		return new BaseDevElement(sb.toString());
	}
	
	@JsonIgnore
	public DevElement getOracleFieldElement() {
		
		String fieldType = this.getFieldType();
		if (StringUtils.equals(fieldType, "VARCHAR")) {
			fieldType = "VARCHAR2";
		}
		
		StringBuilder sb = new StringBuilder(12);
		sb.append("\t")
			.append(this.getFieldName())
			.append(" ")
			.append(this.getFieldType());
		if (this.getFieldLength() !=null && this.getFieldLength() != 0) {
			sb.append("(")
				.append(this.getFieldLength())
				.append(")");
		}
		if (!this.allowNull()) {
			sb.append(" ")
				.append("not null");
		}
		sb.append(",\n");
		return new BaseDevElement(sb.toString());
	}
}
